<?php

abstract class Ratings {
    public static $membre;
    public static $listeTank;
    public static $stats;
    public static $statsRangs = array('ER' => array(600 => 'bad',
                                                     900 => 'baverage',
                                                     1200 => 'average',
                                                     1500 => 'good',
                                                     1800 => 'great',
                                                     9999 => 'unicum'),
                                      'WN7' => array(500 => 'vbad',
                                                     699 => 'bad',
                                                     899 => 'baverage',
                                                     1099 => 'average',
                                                     1349 => 'good',
                                                     1549 => 'vgood',
                                                     1849 => 'great',
                                                     2049 => 'unicum',
                                                     9999 => 'sunicum'),
                                      'WN8' => array(300 => 'vbad',
                                                     599 => 'bad',
                                                     899 => 'baverage',
                                                     1249 => 'average',
                                                     1599 => 'good',
                                                     1899 => 'vgood',
                                                     2349 => 'great',
                                                     2899 => 'unicum',
                                                     9999 => 'sunicum')
    );
    
    public static function calculerER($stats) {
        if ($stats['battles'] === 0) {
            return '--';
        }
        $damage = ($stats['damage_dealt']/$stats['battles']);
        $frags = ($stats['frags']/$stats['battles']);
        $tier = $stats['tiers'];
        $spot = ($stats['spotted']/$stats['battles']);
        $cap = ($stats['capture_points']/$stats['battles']);
        $def = ($stats['dropped_capture_points']/$stats['battles']);
        $er = $damage*(10/($tier + 2))*((0.23-$tier/100)+3*$tier/100);
        $er+= $frags*250;
        $er+= $spot*150;
        $er+= log($cap+1, 1.732)*150;
        $er+= $def*150;
        return floor($er);
    }
    
    public static function calculerWN7($stats) {
        if ($stats['battles'] === 0) {
            return '--';
        }
        $damage = $stats['damage_dealt']/$stats['battles'];
        $frags = $stats['frags']/$stats['battles'];
        $tier = $stats['tiers'];
        $spot = $stats['spotted']/$stats['battles'];
        $cap = $stats['capture_points']/$stats['battles'];
        $def = $stats['dropped_capture_points']/$stats['battles'];
        $battles = $stats['battles'];
        $winrate = $stats['winRate'];
        $wn7 = (1240-1040/pow(min($tier,6),0.164))*$frags;
        $wn7+= $damage*530/(184*exp(0.24*$tier)+130);
        $wn7+= $spot*125*min($tier, 3)/3;
        $wn7+= min($def,2.2)*100;
        $wn7+=((185/(0.17+exp(($winrate-35)*-0.134)))-500)*.45;
        $wn7-=((5-min($tier,5))*125)/(1+exp(($tier-($battles/220)^(3/$tier))*1.5));
        return floor($wn7);
    }
    
    public static function calculerWN8($stats, $accountId= false, $listeTank= null) {
        global $application; 
        $url = 'http://api.worldoftanks.eu/wot/account/tanks/?application_id='.$_SESSION['appId'].'&language=fr&fields=tank_id,statistics&account_id=';
        $membersList = $accountId;
        if ($accountId === false) {
            $membersList = implode(',', array_values(Model_Clan::getMembres($_SESSION['accountInfo']['clanId'])));
        }
        if ($listeTank === null) {
            $listeTank = json_decode(Model_WotHub::api_get_content($url.$membersList, 'listeTank.json'), true)['data'];
        }
        $expValuesFile = json_decode(file_get_contents($application['baseUri'].'/data/expected_tank_values_latest.json'), true);
        foreach ($expValuesFile['data'] as $expTank) {
            $expTankValues[$expTank['IDNum']] = $expTank;
        }
        
        $RatingWN8_TotalDamage = $stats['damage_dealt'];
        $RatingWN8_TotalFrag = $stats['frags'];
        $RatingWN8_TotalSpot = $stats['spotted'];
        $RatingWN8_TotalDef = $stats['dropped_capture_points'];
        $RatingWN8_TotalWins = $stats['wins'];
        $RatingWN8_TotalBattles = $stats['battles'];
        $nb = $RatingWN8_ExpDamage= $RatingWN8_ExpFrag=$RatingWN8_ExpSpot=$RatingWN8_ExpDef=$RatingWN8_ExpWinrate=0;
        foreach ($listeTank as $membre) {
            foreach ($membre as $tank) {
                if (isset($expTankValues[$tank['tank_id']]) === true) {
                    $tankBattles = $tank['statistics']['battles'];
                    $RatingWN8_ExpDamage += $expTankValues[$tank['tank_id']]['expDamage'] * $tankBattles;
                    $RatingWN8_ExpFrag += $expTankValues[$tank['tank_id']]['expFrag'] * $tankBattles;
                    $RatingWN8_ExpSpot += $expTankValues[$tank['tank_id']]['expSpot'] * $tankBattles;
                    $RatingWN8_ExpDef += $expTankValues[$tank['tank_id']]['expDef'] * $tankBattles;
                    $RatingWN8_ExpWinrate += ($expTankValues[$tank['tank_id']]['expWinRate'])/100 * $tankBattles;
                }
            }
        }
        
        $rDAMAGE = $RatingWN8_TotalDamage / $RatingWN8_ExpDamage;
        $rFRAG = $RatingWN8_TotalFrag/ $RatingWN8_ExpFrag;
        $rSPOT = $RatingWN8_TotalSpot / $RatingWN8_ExpSpot;
        $rDEF = $RatingWN8_TotalDef / $RatingWN8_ExpDef;
        $rWIN = $RatingWN8_TotalWins / $RatingWN8_ExpWinrate;
        
        $rDAMAGEc = Max(0, ($rDAMAGE - 0.22) / (1 - 0.22));
        $rFRAGc = Max(0, Min($rDAMAGEc + 0.2, ($rFRAG - 0.12) / (1 - 0.12)));
        $rSPOTc = Max(0, Min($rDAMAGEc + 0.1, ($rSPOT - 0.38) / (1 - 0.38)));
        $rDEFc = Max(0, Min($rDAMAGEc + 0.1, ($rDEF - 0.10) / (1 - 0.10)));
        $rWINc = Max(0, ($rWIN - 0.71) / (1 - 0.71));
        
        $eDAMAGE = 980 * $rDAMAGEc;
        $eFRAG = 210 * $rDAMAGEc * $rFRAGc;
        $eSPOT = 155 * $rFRAGc * $rSPOTc;
        $eDEF = 75 * $rDEFc * $rFRAGc;
        $eWIN = 145 * Min(1.8, $rWINc);
        
        $WN8 = $eDAMAGE +$eFRAG + $eSPOT + $eDEF + $eWIN;
        return floor($WN8);
    }
    
    public static function calculerTankWN8($expStat, $stats) {
        if ($stats['battles'] === 0) {
            return '--';
        }
        
        $RatingWN8_TotalDamage = $stats['damage_dealt'];
        $RatingWN8_TotalFrag = $stats['frags'];
        $RatingWN8_TotalSpot = $stats['spotted'];
        $RatingWN8_TotalDef = $stats['dropped_capture_points'];
        $RatingWN8_TotalWins = $stats['wins'];
        $RatingWN8_TotalBattles = $stats['battles'];
       
        $tankBattles = $stats['battles'];
        $RatingWN8_ExpDamage = $expStat['expDamage'] * $tankBattles;
        $RatingWN8_ExpFrag = $expStat['expFrag'] * $tankBattles;
        $RatingWN8_ExpSpot = $expStat['expSpot'] * $tankBattles;
        $RatingWN8_ExpDef = $expStat['expDef'] * $tankBattles;
        $RatingWN8_ExpWinrate = ($expStat['expWinRate'])/100 * $tankBattles;
        
        $rDAMAGE = $RatingWN8_TotalDamage / $RatingWN8_ExpDamage;
        $rFRAG = $RatingWN8_TotalFrag/ $RatingWN8_ExpFrag;
        $rSPOT = $RatingWN8_TotalSpot / $RatingWN8_ExpSpot;
        $rDEF = $RatingWN8_TotalDef / $RatingWN8_ExpDef;
        $rWIN = $RatingWN8_TotalWins / $RatingWN8_ExpWinrate;
        
        $rDAMAGEc = Max(0, ($rDAMAGE - 0.22) / (1 - 0.22));
        $rFRAGc = Max(0, Min($rDAMAGEc + 0.2, ($rFRAG - 0.12) / (1 - 0.12)));
        $rSPOTc = Max(0, Min($rDAMAGEc + 0.1, ($rSPOT - 0.38) / (1 - 0.38)));
        $rDEFc = Max(0, Min($rDAMAGEc + 0.1, ($rDEF - 0.10) / (1 - 0.10)));
        $rWINc = Max(0, ($rWIN - 0.71) / (1 - 0.71));
        
        $eDAMAGE = 980 * $rDAMAGEc;
        $eFRAG = 210 * $rDAMAGEc * $rFRAGc;
        $eSPOT = 155 * $rFRAGc * $rSPOTc;
        $eDEF = 75 * $rDEFc * $rFRAGc;
        $eWIN = 145 * Min(1.8, $rWINc);
        
        $WN8 = $eDAMAGE +$eFRAG + $eSPOT + $eDEF + $eWIN;
        return floor($WN8);
    }
    
    public  static function addRatios($stats, $accountId= false) {
        $stats['ER'] = Ratings::calculerER($stats);
        $stats['WN7'] = Ratings::calculerWN7($stats);
        $stats['WN8'] = Ratings::calculerWN8($stats, $accountId);
        return $stats;
    }
    
    public static function getAvgTier($accountId) {
        $url = 'http://api.worldoftanks.eu/wot/account/tanks/?application_id='.$_SESSION['appId'].'&language=fr&fields=tank_id,statistics&account_id=';
        $listeTank = json_decode(Model_WotHub::api_get_content($url.$accountId, 'clanmembertank.json'), true);
        $tanksLevel = self::_getTankLevelList();
        $nb = 0;
        $level = 0;
        foreach ($listeTank['data'] as $account => $member) {
            foreach ($member as $tank) {
                $nb+= $tank['statistics']['battles'];
                $level+= $tanksLevel[$tank['tank_id']]*$tank['statistics']['battles'];
            }
            $membreAvgTier[$account] = round($level/$nb,2);
        }
        return $membreAvgTier;
    }
    
    private static function _getTankLevelList() { 
        global $application;
        $pdo = new smartbokModele($application['bdd']);
        $tankList = $pdo->executerSql('SELECT tank_id, level FROM tanks');
        $tanks = array();
        foreach ($tankList as $tank) {
            $tanks[$tank['tank_id']] = $tank['level'];
        }
        return $tanks;
    }
    
    public static function setTankExpStat($expValues, $tank) {
        $expTank['battles'] = '--';
        $expTank['wins'] = floor($tank['all']['battles']*$expValues['expWinRate']/100);
        $expTank['winRate'] = $expValues['expWinRate']; 
        $expTank['draws'] = '--';
        $expTank['losses'] = $tank['all']['battles']-$expTank['wins'];
        $expTank['survived_battles'] = '--';
        $expTank['spotted'] = floor($tank['all']['battles']*$expValues['expSpot']);
        $expTank['shots'] = '--';
        $expTank['hits'] = '--';
        $expTank['hits_percents'] = '--';
        $expTank['frags'] = floor($tank['all']['battles']*$expValues['expFrag']);
        $expTank['xp'] = '--';
        $expTank['battle_avg_xp'] = '--';
        $expTank['damage_dealt'] = floor($tank['all']['battles']*$expValues['expDamage']);
        $expTank['damage_received'] = '--';
        $expTank['capture_points'] = '--';
        $expTank['dropped_capture_points'] = floor($tank['all']['battles']*$expValues['expDef']);
        $expTank['tiers'] = '--';
        return $expTank;
    }
    
    public static function getExpValues() {
        $expValuesFile = json_decode(Model_WotHub::api_get_content('http://www.wnefficiency.net/exp/expected_tank_values_latest.json', 'expected_tank_values_latest.json', false), true);
        foreach($expValuesFile['data'] as $tank) {
            $expvalues[$tank['IDNum']] = $tank;
        }
        return $expvalues;
    }
    
    public static function getRatingRang($rating, $value) {
        foreach (self::$statsRangs[$rating] as $rang => $status) {
            if ((int)$value <= $rang) {
                return $status;
            }
        }
    }
}
		